package com.wzx.user.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.common.constants.SystemConstants;
import com.wzx.common.exception.LeadNewsException;
import com.wzx.common.exception.NeedCodeException;
import com.wzx.common.pojo.TokenRole;
import com.wzx.common.pojo.UserToken;
import com.wzx.common.util.NicknameUtils;
import com.wzx.common.util.date.DateUtil;
import com.wzx.common.util.encrypt.Md5Util;
import com.wzx.common.util.token.AppJwtUtil;
import com.wzx.common.util.username.RandomAccountUtil;
import com.wzx.user.common.RedisPwdErrorNumCommon;
import com.wzx.user.constants.BusinessCon;
import com.wzx.user.dto.UpdateUserDto;
import com.wzx.user.dto.UserDto;
import com.wzx.user.mapper.RelationMapper;
import com.wzx.user.mapper.UserMapper;
import com.wzx.user.mapper.UserVisitorMapper;
import com.wzx.user.mapper.WalletMapper;
import com.wzx.user.pojo.Relation;
import com.wzx.user.pojo.User;
import com.wzx.user.pojo.UserVisitor;
import com.wzx.user.pojo.Wallet;
import com.wzx.user.sdk.common.oss.OssTemplate;
import com.wzx.user.sdk.utils.SmsUtils;
import com.wzx.user.service.UserServcie;
import com.wzx.user.service.WalletService;
import com.wzx.user.utils.AmountUtils;
import com.wzx.user.vo.RegisterVo;
import com.wzx.user.vo.UserLoginVo;
import com.wzx.user.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/4/18 - 19:28
 * @description 标题
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserServcie {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RelationMapper relationMapper;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private UserVisitorMapper userVisitorMapper;

    @Autowired
    private RedisPwdErrorNumCommon redisPwdErrorNumCommon;

    @Autowired
    private WalletMapper walletMapper;

    public static final Integer EXCEED_THE_TIME_LIMIT = AppJwtUtil.TOKEN_TIME_OUT * 24 * 7;


    @Override
    public UserDto login(UserLoginVo userLoginVo) throws LeadNewsException, NeedCodeException {
        //1，根据手机号查询用户是否存在
        LambdaQueryWrapper<User> lambdaQueryWrapper = new QueryWrapper<User>().lambda();
        lambdaQueryWrapper.eq(User::getUsername, userLoginVo.getUsername())
                .or()
                .eq(User::getPhone, userLoginVo.getUsername());

        User userOne = userMapper.selectOne(lambdaQueryWrapper);
        if (StringUtils.isEmpty(userOne)) {
            throw new LeadNewsException(BusinessCon.Login.NOT_FIND_USER);
        }
        if (!StringUtils.isEmpty(userOne.getAccountType()) && "2".equals(userOne.getAccountType())) {
            throw new LeadNewsException("当前用户已被封禁！");
        }

        //2，查询用户存在，返回用户id，根据id查询redis
        String redisLoginKey = BusinessCon.Login.LOGIN_PWD_PREFIX + userOne.getUserId();
        UserDto userDto = new UserDto();
        Integer userLoginNum = (Integer) redisTemplate.opsForValue().get(redisLoginKey);
        String isNeedCode = BusinessCon.Login.LEAD_NEWS_EXCEPTION;
        log.info("用户：{}，登录次数当前是：{}", userOne.getUserId(), userLoginNum);
        //如果是null，则说明是第一次登录，直接设置初始值。
        if (StringUtils.isEmpty(userLoginNum)) {
            //设置值为1，
            redisTemplate.opsForValue().set(redisLoginKey, 1);
            redisTemplate.expire(redisLoginKey, 12, TimeUnit.HOURS);
        } else {
            switch (userLoginNum) {
                case 1:
                    //此时为，第二次，不需要进行任何操作
                    break;
                case 2:
                    //此时为第三次操作，需要向前端返回，短信验证。
                    isNeedCode = BusinessCon.Login.NEED_CODE_EXCEPTION;
                    break;
                default:
                    isNeedCode = BusinessCon.Login.NEED_CODE_EXCEPTION;
                    //三次以上操作，需要验证，短信验证码。
                    if (StringUtils.isEmpty(userLoginVo.getCode())) {
                        //没有带入验证码
                        throwException(isNeedCode, "登录需要验证码！");
                        //throw new LeadNewsException("登录需要验证码！");
                    } else {
                        //登录，带有验证码，获取验证码
                        String code = stringRedisTemplate.opsForValue().get(BusinessCon.User.CODE_HEAD + userLoginVo.getUsername());

                        if (StringUtils.isEmpty(code)) {
                            //throw new LeadNewsException("登录需要发送验证码，请先发送验证码！");
                            throwException(isNeedCode, "登录需要发送验证码，请先发送验证码！");
                        }
                        if (!code.equals(userLoginVo.getCode())) {
                            //throw new LeadNewsException("验证码校验失败！");
                            throwException(isNeedCode, "验证码校验失败！");
                        }
                    }
            }

            //3，登录次数自增
            redisTemplate.opsForValue().increment(redisLoginKey, 1);
            //这里设置12小时过期
            redisTemplate.expire(redisLoginKey, 12, TimeUnit.HOURS);
        }

        boolean verify = Md5Util.verifyByKey(userLoginVo.getPassword(), userOne.getPassword(), userOne.getSalt() + userOne.getUsername());
        if (!verify) {
            throwException(isNeedCode, "用户密码错误！");
            //throw new LeadNewsException("用户密码错误！");
        }

        //生成token
        UserToken userToken = new UserToken(userOne.getUserId(),
                userOne.getHead(),
                userOne.getUsername(),
                userOne.getPhone(),
                TokenRole.getByCode(TokenRole.ROLE_MEDIA.code()),
                userLoginVo.getDeviceType()
        );
        String token = AppJwtUtil.createTokenUserToken(userToken);

        BeanUtils.copyProperties(userOne, userDto);
        userDto.setToken(token);

        //存储到redis中
        if (StringUtils.isEmpty(userLoginVo.getDeviceType())) {
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + userOne.getUserId(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
        } else {
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + userOne.getUserId() + userLoginVo.getDeviceType(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
        }

        //登录成功，删除
        redisTemplate.delete(redisLoginKey);
        log.info("用户：{}，登录成功！", userOne.getUserId());
        return userDto;
    }

    public void throwException(String exceptionType, String msg) throws LeadNewsException, NeedCodeException {
        if ("LeadNewsException".equals(exceptionType)) {
            throw new LeadNewsException(msg);
        } else if ("NeedCodeException".equals(exceptionType)) {
            throw new NeedCodeException(msg);
        }
    }


    @Override
    public UserDto loginByCode(RegisterVo registerVo) throws LeadNewsException, NeedCodeException {
        //1，根据手机号查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", registerVo.getPhone());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new LeadNewsException("该用户不存在，请注册");
        }
        if (!StringUtils.isEmpty(user.getAccountType()) && "2".equals(user.getAccountType())) {
            throw new LeadNewsException("当前用户已被封禁！");
        }

        //2，验证码次数校验
        String redisLoginKey = BusinessCon.Login.LOGIN_CODE_PREFIX + registerVo.getPhone();
        Integer userLoginNum = (Integer) redisTemplate.opsForValue().get(redisLoginKey);
        if (!StringUtils.isEmpty(userLoginNum) && userLoginNum >= 3) {
            //验证码需要重置，重新生成验证码
            throw new LeadNewsException("验证码校验失败次错过多，请重新发送验证码！");
        }

        log.info("用户：{}，登录次数当前是：{}", user.getUserId(), userLoginNum);


        //3，检验验证码
        String code = stringRedisTemplate.opsForValue().get(BusinessCon.User.CODE_HEAD + registerVo.getPhone());

        if (code == null || !code.equals(registerVo.getCode())) {
            //登录次数自增
            redisTemplate.opsForValue().increment(redisLoginKey, 1);
            //这里设置12小时过期
            redisTemplate.expire(redisLoginKey, 12, TimeUnit.HOURS);

            throw new LeadNewsException("验证码错误！");
        }

        //4，生成token
        UserToken userToken = new UserToken(user.getUserId(),
                user.getHead(),
                user.getUsername(),
                user.getPhone(),
                TokenRole.getByCode(TokenRole.ROLE_MEDIA.code()),
                registerVo.getDeviceType());
        String token = AppJwtUtil.createTokenUserToken(userToken);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        userDto.setToken(token);

        //5，存储到redis中
        if (StringUtils.isEmpty(registerVo.getDeviceType())) {
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + user.getUserId(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
        } else {
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + user.getUserId() + registerVo.getDeviceType(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
        }

        //6，登录成功，清空验证码校验次数
        redisTemplate.delete(redisLoginKey);
        log.info("用户：{}，登录成功！", user.getUserId());

        return userDto;
    }

    @Override
    public void updatePwd(UpdateUserDto updateUserDto) throws LeadNewsException {
        redisPwdErrorNumCommon.getPwdErrorNum(updateUserDto.getUserId(), 2);

        //1，搜索用户是否存在
        User user = userMapper.selectById(updateUserDto.getUserId());
        if (StringUtils.isEmpty(user)) {
            throw new LeadNewsException("用户不存在！");
        }

        //2，对比密码是否正确
        boolean verify = Md5Util.verifyByKey(updateUserDto.getPassword(), user.getPassword(), user.getSalt() + user.getUsername());
        if (!verify) {
            redisPwdErrorNumCommon.setPwdErrorNum(updateUserDto.getUserId(), 2);
            throw new LeadNewsException("用户密码错误！");
        }
        redisPwdErrorNumCommon.closePwdErrorNum(updateUserDto.getUserId(), 2);

        //3，修改密码
        String md5Pwd = Md5Util.md5byKey(updateUserDto.getNewPassword(), user.getSalt() + user.getUsername());
        //System.out.println("md5Pwd = " + md5Pwd);
        user.setPassword(md5Pwd);
        //System.out.println("user = " + user);
        userMapper.updateById(user);
    }

    @Override
    public void forwardPwdSendCode(String phone, Integer userId) throws LeadNewsException, ClientException {
        //判断用户手机号是否匹配
        User user = userMapper.selectById(userId);
        if (StringUtils.isEmpty(user)) {
            throw new LeadNewsException("用户不存在！");
        }
        if (!user.getPhone().equals(phone)) {
            throw new LeadNewsException("手机号不匹配！");
        }
        //发送验证码
        getYzm(phone);
    }

    @Override
    public void forwardPwd(UpdateUserDto updateUserDto) throws LeadNewsException {
        redisPwdErrorNumCommon.getYzmErrorNum(updateUserDto.getPhone());

        //1，检验验证码
        //System.err.println(updateUserDto.getPhone());
        String code = stringRedisTemplate.opsForValue().get(BusinessCon.User.CODE_HEAD + updateUserDto.getPhone());

        //System.out.println("code = " + code);
        if (code == null || !code.equals(updateUserDto.getCode())) {
            redisPwdErrorNumCommon.setYzmErrorNum(updateUserDto.getPhone());
            throw new LeadNewsException("验证码错误！");
        }
        redisPwdErrorNumCommon.closeYzmErrorNum(updateUserDto.getPhone());
        //2，修改密码
        User user = userMapper.selectById(updateUserDto.getUserId());
        if (StringUtils.isEmpty(user)) {
            throw new LeadNewsException("用户不存在！");
        }


        String md5Pwd = Md5Util.md5byKey(updateUserDto.getNewPassword(), user.getSalt() + user.getUsername());
        user.setPassword(md5Pwd);
        userMapper.updateById(user);
    }

    @Override
    public void logOff(Integer userId) {
        //删除用户表
        userMapper.deleteById(userId);
        //删除关联关系
        LambdaQueryWrapper<Relation> lambdaQueryWrapper = new QueryWrapper<Relation>().lambda();
        lambdaQueryWrapper.eq(Relation::getUid, userId)
                .or()
                .eq(Relation::getFid, userId);
        lambdaQueryWrapper.eq(Relation::getType, "2");
        relationMapper.delete(lambdaQueryWrapper);
        //删除发布信息

    }

    @Override
    public UserDto loginByIphone(String phone) throws LeadNewsException {
        //1，判断用户是否注册
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User userOne = userMapper.selectOne(queryWrapper);

        if (!StringUtils.isEmpty(userOne)) {
            //2，不需要注册，直接返回信息
            //生成token
            UserToken userToken = new UserToken(userOne.getUserId(),
                    userOne.getHead(),
                    userOne.getUsername(),
                    userOne.getPhone(),
                    TokenRole.getByCode(TokenRole.ROLE_MEDIA.code()),
                    "iphone"
            );
            String token = AppJwtUtil.createTokenUserToken(userToken);
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userOne, userDto);
            userDto.setToken(token);

            //存储到redis中
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + userOne.getUserId(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
            return userDto;
        } else {
            //3，需要注册
            //3.1，获取昵称
            String name = NicknameUtils.getName();

            //3.2，生成随机账号
            String username = getUsername();

            //3.3，插入数据
            User user = new User();
            user.setNickname(name);
            user.setUsername(username);
            user.setCreateTime(DateUtil.getDate());
            user.setSalt(System.currentTimeMillis() + "");
            //3.4，设置初始头像
            user.setHead("https://lover-wzx.oss-cn-shenzhen.aliyuncs.com/01.png");

            //3.5，设置用户未实名
            user.setAuthentication("0");

            //3.6，生成用户默认生日
            user.setBirthday("2000-01-01");

            //3.7，插入数据库
            userMapper.insert(user);

            //3.8，生成token
            UserToken userToken = new UserToken(user.getUserId(),
                    user.getHead(),
                    user.getUsername(),
                    user.getPhone(),
                    TokenRole.getByCode(TokenRole.ROLE_MEDIA.code()),
                    "iphone"
            );
            String token = AppJwtUtil.createTokenUserToken(userToken);
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(user, userDto);
            userDto.setToken(token);

            //存储到redis中
            stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + user.getUserId(), token, EXCEED_THE_TIME_LIMIT, TimeUnit.SECONDS);
            return userDto;
        }
    }

    @Override
    public UserDto loginByVisitor() {

        UserVisitor userVisitor = new UserVisitor();
        userVisitor.setUsername(System.currentTimeMillis() + "");
        userVisitor.setNickname(NicknameUtils.getName());
        userVisitor.setHead("https://lover-wzx.oss-cn-shenzhen.aliyuncs.com/01.png");
        userVisitor.setPhone("199899999");
        userVisitorMapper.insert(userVisitor);

        UserToken userToken = new UserToken(userVisitor.getUserId(),
                userVisitor.getHead(),
                userVisitor.getNickname(),
                userVisitor.getPhone(),
                TokenRole.getByCode(TokenRole.ROLE_MEDIA.code()),
                "test"
        );
        String token = AppJwtUtil.createTokenUserToken(userToken);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userVisitor, userDto);
        userDto.setToken(token);

        //存储到redis中
        stringRedisTemplate.opsForValue().set(SystemConstants.REDIS_TOKEN_APP_PREFIX + userVisitor.getUserId() + "test", token, AppJwtUtil.TOKEN_TIME_OUT * 24, TimeUnit.SECONDS);
        return userDto;
    }

    @Override
    public String queryUserImgCode(String phone) {
        //1，使用工具生成 图片验证码


        return null;
    }

    @Override
    public UserDto loginByUserAmount(UserLoginVo userLoginVo) throws LeadNewsException, NeedCodeException {
        UserDto userDto = login(userLoginVo);
        //查询用户余额
        //查询用户余额
        QueryWrapper<Wallet> walletQueryWrapper = new QueryWrapper<>();
        walletQueryWrapper.eq("uid", userDto.getUserId());
        Wallet wallet = walletMapper.selectOne(walletQueryWrapper);
        if (wallet == null || wallet.getBalance() == null) {
            userDto.setAmount((double) 0);
        } else {
            userDto.setAmount(AmountUtils.handlerAmountByDouble(wallet.getBalance()));
        }
        return userDto;
    }

    @Override
    public int queryCountUser() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public String register(RegisterVo registerVo) throws LeadNewsException {
        //0，根据 nickname判断昵称是否重复
        QueryWrapper<User> queryWrapperByNickname = new QueryWrapper<>();
        queryWrapperByNickname.eq("nickname", registerVo.getNickname());
        Integer countByNick = userMapper.selectCount(queryWrapperByNickname);
        if (countByNick > 0) {
            throw new LeadNewsException("昵称已存在！");
        }
        //1，判断该用户的验证码是否正确
        //获取Redis中验证码
        String code = stringRedisTemplate.opsForValue().get(BusinessCon.User.CODE_HEAD + registerVo.getPhone());
        if (code == null || !code.equals(registerVo.getCode())) {
            throw new LeadNewsException("验证码错误！");
        }

        //2，判断该用户对应的手机号是否有创建过账号
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", registerVo.getPhone());
        Integer count = userMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new LeadNewsException("该用户已创建过账号！");
        }

        //3，生成随机账号
        String username = getUsername();

        //4，插入数据
        User user = new User();
        BeanUtils.copyProperties(registerVo, user);
        user.setUsername(username);
        user.setCreateTime(DateUtil.getDate());
        user.setSalt(System.currentTimeMillis() + "");
        //设置初始头像
        user.setHead("https://lover-wzx.oss-cn-shenzhen.aliyuncs.com/01.png");

        //5，密码加密
        String md5Pwd = Md5Util.md5byKey(user.getPassword(), user.getSalt() + user.getUsername());
        user.setPassword(md5Pwd);

        //6，设置用户未实名
        user.setAuthentication("0");

        //7，生成用户默认生日
        user.setBirthday("2000-01-01");

        userMapper.insert(user);
        return username;
    }

    @Override
    public String reset(RegisterVo registerVo) throws LeadNewsException {
        //1，根据userName查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", registerVo.getPhone());
        User userOne = userMapper.selectOne(queryWrapper);
        if (StringUtils.isEmpty(userOne)) {
            throw new LeadNewsException("用户不存在！");
        }

        String code = stringRedisTemplate.opsForValue().get(BusinessCon.User.CODE_HEAD + registerVo.getPhone());
        if (code == null || !code.equals(registerVo.getCode())) {
            throw new LeadNewsException("验证码错误！");
        }
        userOne.setSalt(System.currentTimeMillis() + "");

        String md5Pwd = Md5Util.md5byKey(registerVo.getPassword(), userOne.getSalt() + userOne.getUsername());
        userOne.setPassword(md5Pwd);

        //修改密码
        userMapper.updateById(userOne);
        return userOne.getUsername();
    }

    @Override
    public UserDto getUserInfo(Integer userId) {
        User user = userMapper.selectById(userId);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user, userDto);
        return userDto;
    }

    @Override
    public void getYzm(Serializable phone) throws LeadNewsException, ClientException {
        long count = stringRedisTemplate.boundValueOps(BusinessCon.User.REQ_NUMBER + phone).increment(1);
        if (count == 1) {
            //设置 5 分钟过期
            stringRedisTemplate.expire(BusinessCon.User.REQ_NUMBER + phone, BusinessCon.User.OVERDUE_Time, TimeUnit.SECONDS);
        }
        //5分钟内连续发送5次短息，则发送频繁
        if (count > 8) {
            throw new LeadNewsException("5分钟内只能发送8次短信");
        }

        String code = generatorSmsCode();
        //String code = "666666";

        stringRedisTemplate.opsForValue().set(BusinessCon.User.CODE_HEAD + phone, code);

        //设置过期时间
        stringRedisTemplate.expire(BusinessCon.User.CODE_HEAD + phone, BusinessCon.User.OVERDUE_Time, TimeUnit.SECONDS);

        //重置验证码，需要清除 验证码登录校验次数
        redisTemplate.delete(BusinessCon.Login.LOGIN_CODE_PREFIX + phone);

        //发送验证码
        smsUtils.sendMessage(phone + "", code);

    }

    @Override
    public boolean updateUserInfo(UserVo userVo) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userVo.getUserId());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new LeadNewsException("找不到用户信息！");
        }
        int count = userMapper.updateUserInfo(userVo);

        return count > 0;

    }

    @Override
    public String updateUserHead(MultipartFile file, long userId) throws LeadNewsException, IOException {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new LeadNewsException("找不到用户信息！");
        }
        //上传头像，得到头像url
        String fileName = file.getOriginalFilename();
        if (StringUtils.isEmpty(fileName)) {
            throw new LeadNewsException("文件名称为空！");
        }
        String head = ossTemplate.upload(fileName, file.getInputStream());
        user.setHead(head);
        userMapper.updateById(user);
        return head;
    }


    /**
     * 生成随机账号
     *
     * @return
     * @throws LeadNewsException
     */
    public String getUsername() throws LeadNewsException {
        while (true) {
            String accountNumber = RandomAccountUtil.randomNumberIsTen(9, 1).toString();
            QueryWrapper<User> queryWrapperByUsername = new QueryWrapper<>();
            queryWrapperByUsername.eq("username", accountNumber);
            Integer count = userMapper.selectCount(queryWrapperByUsername);
            if (count == 0) {
                return accountNumber;
            }
        }
    }

    /**
     * 短信验证码的方法，随机六位数字
     *
     * @return
     */
    private String generatorSmsCode() {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < Integer.parseInt("6"); i++) {
            code.append(new Random().nextInt(10));
        }
        return code.toString();
    }

}
